/** @author dongls */

const IS_PRODUCTION = process.env.NODE_ENV == 'production';
const path = require('path')

const util = require('./util')

const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { VueLoaderPlugin} = require('vue-loader');

const WebpackBar = require('webpackbar');

module.exports = {
  mode: 'development',
  entry: util.genEntry(),
  output: {
    publicPath: '/'
  },
  module: {
    rules: [
      { 
        test: /\.tsx?$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            cacheDirectory: util.getBabelCacheDir(), // 开启缓存
            presets: ['@babel/preset-env'], // babel中内容的转换规则工具
          }
        }
      },
      { 
        test: /\.vue$/,
        loader: 'vue-loader',
      },
      {
        test: /\.m?js$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
        options: {
          cacheDirectory: util.getBabelCacheDir(), // 开启缓存
        },
      },
      // 降级处理，将node_modules中的pub-bbx-utils中的js文件进行babel处理
      {
        test: /\.[mc]?js$/,
        loader: 'babel-loader',
        include: /node_modules[/\\]pub-bbx-utils/
      },
      {
        test: /\.scss$/,
        use: [
          IS_PRODUCTION ? MiniCssExtractPlugin.loader : 'vue-style-loader',
          'css-loader',
          'postcss-loader',
          'css-unicode-loader', // 将 CSS content内容中的双字节字符转换成unicode编码
          { loader: 'sass-loader', 
            options: { 
              implementation: require('sass'), 
              sassOptions: { quietDeps: true } // 去除 warning ！Dart Sass 2.0.0, !global assignments won't be able to declare 
            }
          },
          util.genSassResourceLoader()
        ]
      },
      {
        test: /\.css$/,
        use: [
          IS_PRODUCTION ? MiniCssExtractPlugin.loader : 'vue-style-loader',
          'css-loader',
          'postcss-loader',
          'css-unicode-loader' // 将 CSS content内容中的双字节字符转换成unicode编码
        ]
      },
      { // 处理字体,gif
        test: /\.(eot|svg|ttf|woff|woff2)(\?\S*)?$/,
        type: 'asset/resource',
        exclude: path.resolve(__dirname, '../src/assets/svg'),
      },
      {
        test: /\.(png|jpe?g)(\?.*)?$/,
        type: 'asset/resource',
      },
      { // gif单独处理
        test: /\.(gif)(\?.*)?$/,
        type: 'asset/resource',
      },
      { // mp3单独处理
        test: /\.(mp3)(\?.*)?$/,
        type: 'asset/resource',
      },
      { // svg图像，所有svg都需要放入该目录下，负责会被当做字体处理
        test: /\.(svg)(\?.*)?$/,
        include: path.resolve(__dirname, '../src/assets/svg'),
        type: 'asset/inline'
      },
    ]
  },
  plugins: [
    new WebpackBar(),
    new VueLoaderPlugin(),
  ],
  
  resolve: {
    extensions: ['.js', '.json', '.vue', '.ts', '.jsx', '.tsx', '.scss', '.css'],
    alias: {
      'vue$': 'vue/dist/vue.runtime.esm.js',
      '@src': path.join(__dirname, '../src'),
      '@form': path.join(__dirname, '../src/component/form'),
      '@formComponents': path.join(__dirname, '../src/component/form/components'),
      '@model': path.join(__dirname, '../model'),
      '@service': path.join(__dirname, '../service'),
      '@server': path.join(__dirname, '../server'),
      '@hooks': path.join(__dirname, '../hooks'),
      'src': path.join(__dirname, '../src'),
      'packages': path.join(__dirname, '../packages'),
      '@lib': path.join(__dirname, '../lib'),
      "@account":  path.join(__dirname, '../src/modules/account'),
      "@version":  path.join(__dirname, '../src/modules/version'),
      "@gpt":  path.join(__dirname, '../src/modules/setting/gpt'),
      'element-scss': path.join(__dirname, '../node_modules/element-ui/packages/theme-chalk/src'),
    },
    fallback: {
      'url': require.resolve('url/'),
      'qs': require.resolve('qs/'),
    }
  },
  performance: {
    maxEntrypointSize: 2097152
  },
  devtool: IS_PRODUCTION ? false : 'eval-source-map',
}
